package com.tl.commonlibrary.storage.filecache;

import android.content.Context;
import android.text.TextUtils;

import com.tl.commonlibrary.tool.MD5Util;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

/**
 * Created by jiangzhongshan on 15/7/28.
 */
public class FileCache {
    private DiskLruCache lruCache;
    private CacheConfig cacheConfig;

    public FileCache(Context context) {
        cacheConfig = new CacheConfig(context);
    }
    public FileCache(File cachePathFile){
        cacheConfig = new CacheConfig(cachePathFile);
    }
    public FileCache(CacheConfig config){
        cacheConfig = config;
    }
    public CacheConfig getCacheConfig() {
        return cacheConfig;
    }

    public void setCacheConfig(CacheConfig cacheConfig) {
        if (cacheConfig == null) {
            return;
        }
        this.cacheConfig = cacheConfig;
    }

    public void open() {
        try {
            if(cacheConfig.getCacheDir() != null && cacheConfig.getCacheSize()>0) {
                lruCache = DiskLruCache.open(cacheConfig.getCacheDir(), 1, 1, cacheConfig.getCacheSize());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void save(String key, String data) {
        if(TextUtils.isEmpty(key) || TextUtils.isEmpty(data)){
            return;
        }
        if (lruCache != null) {
            key = MD5Util.getMD5(key);
            data = MD5Util.JM(data);
            OutputStream outputStream = null;
            try {
                DiskLruCache.Editor editor = lruCache.edit(key);
                outputStream = editor.newOutputStream(0);
                if (outputStream != null) {
                    outputStream.write(data.getBytes(Charset.forName("UTF-8")));
                }
                editor.commit();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }

    public void append(String key, String data) {
        if(TextUtils.isEmpty(key) || TextUtils.isEmpty(data)){
            return;
        }
        key = MD5Util.getMD5(key);
        data = MD5Util.JM(data);
        OutputStream outputStream = null;
        BufferedWriter writer = null;
        if (lruCache != null) {
            try {
                DiskLruCache.Editor editor = lruCache.edit(key);
                outputStream = editor.newOutputStream(0);
                writer = new BufferedWriter(new OutputStreamWriter(outputStream));
                writer.append(data);
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    outputStream.close();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public String load(String key) {
        if(TextUtils.isEmpty(key)){
            return null;
        }
        key = MD5Util.getMD5(key);
        InputStream inputStream = null;
        if(lruCache != null)
        {
            try {
                DiskLruCache.Snapshot snapshot = lruCache.get(key);
                if(snapshot != null) {
                    inputStream = snapshot.getInputStream(0);
                    if(inputStream != null){
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        int r =-1;
                        while((r=inputStream.read())!=-1){
                            baos.write(r);
                        }
                        return MD5Util.KL(baos.toString());
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public void close(){
        if(lruCache != null){
            try {
                lruCache.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
